package com.swu.cx.alwayslearning.classloader

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import com.swu.cx.alwayslearning.R

class ClassLoaderActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_class_loader)
        findViewById<Button>(R.id.btn_do).setOnClickListener {
          JavaTest.test()
        }
    }

    fun testReflect() {
        val s = Student()
        val clz = Class.forName("com.swu.cx.alwayslearning.classloader.Student")
        val hobbyFiled = clz.getDeclaredField("hobbies").apply {
            isAccessible = true
        }
        val hobbies = hobbyFiled.get(s) as Array<String>
        Log.e("cx","-------------before start----------------")
        hobbies.forEach {
            Log.e("cx", it)
        }
        Log.e("cx","-------------before end----------------")

        val newHobbies = arrayOfNulls<String>(hobbies.size + 1)
        var index = 0
        newHobbies[index++] = "......"
        hobbies.forEach {
            newHobbies[index++] = it
        }
        hobbyFiled.set(s, newHobbies)
        Log.e("cx","-------------after start----------------")
        (hobbyFiled.get(s) as Array<String>).forEach {
            Log.e("cx", it)
        }
        Log.e("cx","-------------after end----------------")
    }
}